﻿//Prevent the TAB key in boxes
function checkTab(){
  if (event.keyCode == 9) { 
	event.returnValue = false;
  }
}
//Go to next box as it reach the max chars
function gotoMax(thisbox,max,maxvalue,next){
    boxvalue=eval(thisbox+'.value');
	function focusClear(){
		eval(thisbox+'.value="";');
		eval(thisbox+'.focus();');
	}
	//Is a number?
    if (isNaN(boxvalue)){
        alert(OPT_NAN);
	focusClear();
    }
	if (boxvalue>maxvalue){
		alert(OPT_HIGH_VALUE);
		focusClear();
	}
	total=eval(thisbox+'.value.length');
	if (total==max){
		eval(next+'.focus()');
	}else if(total>max){
		full=eval(thisbox+'.value');
		cut=full.substring(0,max);
		eval(thisbox+'.value='+cut);
		eval(next+'.focus()');
	}
}
function onOpen() {
  // Get Saved Values
  box_date.focus();
  box_date.value = options.getValue("box_date");
  box_month.value = options.getValue("box_month");
  box_year.value = options.getValue("box_year");
  box_desc.value = options.getValue("box_desc");
  bkgimg=options.getValue("box_img");
  if (bkgimg==''){
	  bkgimg='stock_images\\background.png';
  }
  box_img.src = bkgimg;
  transp=options.getValue("box_transp");
  if (transp==''){
	box_transp.value='200';
  }else{
	box_transp.value=transp;
  }
}
function setDefault(){
	box_img.src='stock_images\\background.png';
}
function onOk() {
  // Save all the settings
  options.putValue("box_date", box_date.value);
  options.putValue("box_month", box_month.value);
  options.putValue("box_year", box_year.value);
  options.putValue("box_desc", box_desc.value);
  options.putValue("box_img", box_img.src);
  options.putValue("box_transp", box_transp.value);
}

/**
 * Drag image here!
 */  
function dragImage() {
  var images = Utils.createDragFilesImagesList(event.dragFiles);
  var numImages = images.length;
  if (numImages>0){
	box_img.src = images[0];
  }
}

/**
 * Utils namespace
 */
var Utils = {};

/**
 * Convert event.dragfiles object to an array of strings
 * @param {Object} obj collection of drag files
 * @return {Array} Array of filepaths
 */
Utils.createDragFilesImagesList = function(obj) {
  var files = [];
  if (!obj) {
    return files;
  }
  var e = new Enumerator(obj);
  var validExtensions = {
    png: true,
    gif: true,
    jpg: true,
    jpeg: true }; 
  while (!e.atEnd()) {
    var path = e.item();
    var extension = Utils.extractExtension(path).toLowerCase();
    if (validExtensions[extension] === true) {
      files.push(path + '');
    }
    e.moveNext();
  }
  return files;
};

/**
 * Extract the extension from a path
 * @param {String} The path
 * @return {String} The extension
 */
Utils.extractExtension = function(s) {
  return s.substring(s.lastIndexOf('.') + 1); 
};